package com.inspur.cloud.demo.util;

/**
 * Created by yuanfq on 2020-07-04.
 */
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;

@JsonInclude(Include.NON_NULL)
@ApiModel(
        description = "接口响应体"
)
public class Response<T> implements Serializable {
    private static final long serialVersionUID = -4505655308965878999L;
    private static final String successCode = "0000";
    @ApiModelProperty("返回数据")
    private T data;
    @ApiModelProperty(
            value = "返回码",
            required = true
    )
    private int status;
    @ApiModelProperty(
            value = "返回描述",
            required = true
    )
    private String message;



    public static <T> ResponseBuilder<T> builder() {
        return new ResponseBuilder();
    }

    public  static <T>  Response success(){
        return Response.builder().status(ResponseStatus.SUCCESS.getCode()).message(ResponseStatus.SUCCESS.getMsg()).build();
    }

    public  static <T>  Response success(T data){
        return Response.builder().status(ResponseStatus.SUCCESS.getCode()).message(ResponseStatus.SUCCESS.getMsg()).data(data).build();
    }

    public static <T> Response fail() {
        return error(ResponseStatus.FAIL);
    }


    public static <T> Response error(ResponseStatus responseStatus) {
        return Response.builder().status(responseStatus.getCode()).message(responseStatus.getMsg()).build();
    }

    public static <T> Response normal(T data) {
        return Response.builder().data(data).build();
    }

    public T getData() {
        return this.data;
    }

    public int getStatus() {
        return this.status;
    }

    public String getMessage() {
        return this.message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    protected boolean canEqual(Object other) {
        return other instanceof Response;
    }


    public String toString() {
        return "Response(data=" + this.getData() + ", status=" + this.getStatus() + ", message=" + this.getMessage() + ")";
    }

    public Response() {
    }

    public Response(T data, int status, String message) {
        this.data = data;
        this.status = status;
        this.message = message;
    }

    public static class ResponseBuilder<T> {
        private T data;
        private int status;
        private String message;

        ResponseBuilder() {
        }

        public ResponseBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public ResponseBuilder<T> status(int status) {
            this.status = status;
            return this;
        }

        public ResponseBuilder<T> message(String message) {
            this.message = message;
            return this;
        }

        public Response<T> build() {
            return new Response(this.data, this.status, this.message);
        }

        public String toString() {
            return "Response.ResponseBuilder(data=" + this.data + ", status=" + this.status + ", message=" + this.message + ")";
        }
    }

    public enum ResponseStatus {
        SUCCESS(200, "成功"),
        FAIL(400, "失败"),
        ARGUMENT_NOT_VALID(198, "参数校验错误"),
        UNAUTHORIZED(401, "未认证（签名错误）"),
        NOT_FOUND(404, "接口不存在"),
        INTERNAL_SERVER_ERROR(500, "服务器内部错误");

        private int code;
        private String msg;

        private ResponseStatus(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return this.code;
        }

        public String getMsg() {
            return this.msg;
        }
    }

}

